added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSWPFDataBinding / MainWindow.xaml
blobfbb5310574baacff05236c13519033626678d152
1 <Window x:Class="CSWPFDataBinding.MainWindow"
2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4     xmlns:local ="clr-namespace:CSWPFDataBinding"
5     Title="MainWindow" Height="600" Width="600">
6     <Window.Resources>
7         
8         <!--Data Providers-->
9         <ObjectDataProvider x:Key="defaultPerson" ObjectType="{x:Type local:Person}"></ObjectDataProvider>
10         <ObjectDataProvider x:Key="persons" ObjectType="{x:Type local:Persons}"></ObjectDataProvider>
11         <CollectionViewSource x:Key="personSource" Source="{StaticResource persons}"></CollectionViewSource>
12         <!--Converters-->
13         <local:SalaryFormmatingConverter x:Key="con"></local:SalaryFormmatingConverter>
14         
15         <!--Board Style-->
16         <Style TargetType="{x:Type Border}" >
17             <Style.Setters>
18                 <Setter Property="BorderBrush" Value="Blue"></Setter>
19                 <Setter Property="BorderThickness" Value="1.0"></Setter>
20                 <Setter Property="Background" Value="White"></Setter>
21                 <Setter Property="CornerRadius" Value="10"></Setter>
22             </Style.Setters>
23             <Style.Triggers>
24                 <Trigger Property="IsMouseOver" Value="True">
25                     <Setter Property="BorderThickness" Value="2.0"></Setter>
26                     <Setter Property="BorderBrush" Value="Yellow"></Setter>
27                 </Trigger>
28             </Style.Triggers>
29         </Style>
30         <!-- Error Template for TextBox-->
31         <ControlTemplate x:Key="errorTemplate">
32             <DockPanel>
33                 <AdornedElementPlaceholder></AdornedElementPlaceholder>
34                 <TextBlock Foreground="Red" FontSize="20" Text="!"></TextBlock>
35             </DockPanel>
36         </ControlTemplate>
37     </Window.Resources>
38     <Grid Background="White">
39         <Grid.ColumnDefinitions>
40             <ColumnDefinition></ColumnDefinition>
41             <ColumnDefinition></ColumnDefinition>
42         </Grid.ColumnDefinitions>
43         <Grid.RowDefinitions>
44             <RowDefinition Height="Auto">
45             </RowDefinition>
46             <RowDefinition Height="Auto">
47             </RowDefinition>
48             <RowDefinition Height="Auto">
49             </RowDefinition>
50             <RowDefinition Height="Auto">
51             </RowDefinition>
52             <RowDefinition Height="Auto">
53             </RowDefinition>
54             <RowDefinition Height="Auto">
55             </RowDefinition>
56             <RowDefinition Height="Auto">
57             </RowDefinition>
58             <RowDefinition Height="Auto">
59             </RowDefinition>
60         </Grid.RowDefinitions>
61         
62         <TextBlock Text="Binding using XAML code:" Grid.Row="0" Grid.ColumnSpan="2"></TextBlock>
63         <TextBlock Text="Binding using Procedure code:" Grid.Row="2" Grid.ColumnSpan="2"></TextBlock>
64         <TextBlock Text="CollectionBinding using Xaml" Grid.Row="4" Grid.ColumnSpan="2"></TextBlock>
65         <TextBlock Text="CollectionBinding using Procedure Code" Grid.Row="6" Grid.ColumnSpan="2"></TextBlock>
66         
67         <Border Grid.Column="0" Grid.Row="1">
68             <StackPanel Margin="10" >
69                 <!--Twoway binding, and the change will be propagated to the source to target once propery is changed.-->
70                 <StackPanel Orientation="Horizontal" >
71                     <Label>Name:</Label>
72                     <TextBox  Text="{Binding  Name, Source ={StaticResource defaultPerson}, Mode=Twoway,
73                                                                 UpdateSourceTrigger=PropertyChanged }" Height="25" Width="222"></TextBox>
74                 </StackPanel>
75                 <!--The default UpdateSourceTrigger property for Text is LostFocus, you will find source will be updated when the Textbox losts focus.
76                     You will see Textbox will not load the defalut value when it is loaded due to it uses OneWayToSouce binding mode.
77                 -->
78                 <StackPanel Orientation="Horizontal"  >
79                     <Label Width="44" Height="25">Job:</Label>
80                     <TextBox   Text="{Binding Job, Source={StaticResource defaultPerson}, Mode = OneWayToSource}" Height="26" Width="222"></TextBox>
81                 </StackPanel>
82                 <!--If age is less than 0, or much bigger than 300. There will be "!" appearing next to TextBox indicating the input error-->
83                 <StackPanel Orientation="Horizontal"  >
84                     <Label Height="26" Width="44">Age:</Label>
85                     <TextBox  Height="26" Width="222" Validation.ErrorTemplate="{StaticResource errorTemplate}">
86                         <TextBox.Text>
87                             <Binding Path="Age" Source="{StaticResource defaultPerson}">
88                                 <Binding.ValidationRules>
89                                     <local:AgeValidationRule></local:AgeValidationRule>
90                                 </Binding.ValidationRules>
91                             </Binding>
92                         </TextBox.Text>
93                     </TextBox>
94                 </StackPanel>
95                 
96                 
97                  <StackPanel Orientation="Horizontal"  >
98                     <Label Height="26" Width="44">Salary:</Label>
99                     <TextBox  Text="{Binding Salary, Source={StaticResource defaultPerson}, Mode=Twoway, UpdateSourceTrigger =PropertyChanged}"
100                                                                     Height="26" Width="222"></TextBox>
101                 </StackPanel>
102                 
103                 <!--The target will be updated only if we change the source if the binding mode is Oneway-->
104                  <StackPanel Orientation="Horizontal"  >
105                     <Label Height="23" Width="53">Interest:</Label>
106                     <TextBox  Text="{Binding Interest, Source={StaticResource defaultPerson}, Mode =Oneway}" Height="26" Width="222"></TextBox>
107                 </StackPanel>
108             </StackPanel>
109         </Border>
110         <Border Grid.Column="1" Grid.Row="1">
111             <StackPanel Margin="10" >
112                 <StackPanel Orientation="Horizontal">
113                     <Label>Name:</Label>
114                     <Label  Content="{Binding Name, Source ={StaticResource defaultPerson}}" Height="25" Width="222"></Label>
115                 </StackPanel>
116                 <StackPanel Orientation="Horizontal">
117                     <Label Width="44" Height="25">Job:</Label>
118                     <Label    Content="{Binding Job, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
119                 </StackPanel>
120                 <StackPanel Orientation="Horizontal">
121                     <Label Height="26" Width="44">Age:</Label>
122                     <Label   Content="{Binding Age, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
123                 </StackPanel>
124                 <StackPanel Orientation="Horizontal"  >
125                     <Label Height="26" Width="44">Salary:</Label>
126                     <!--Here We use the converter to format the salar so that user can see a more readable text-->
127                     <Label  Content="{Binding Salary, Source={StaticResource defaultPerson}, Converter={StaticResource con}}" Height="26" Width="222"></Label>
128                 </StackPanel>
129                 <StackPanel Orientation="Horizontal">
130                     <Label Height="25" Width="56">Interest:</Label>
131                     <Label  Content="{Binding Interest, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
132                 </StackPanel>
133             </StackPanel>
134         </Border>
137         <!--Binding using Procedure code-->
138         <Border Grid.Column="0" Grid.Row="3">
139             <StackPanel Margin="10" >
140                 <!--Twoway binding, and the change will be propagated to the source to target once propery is changed.-->
141                 <StackPanel Orientation="Horizontal" >
142                     <Label>Name:</Label>
143                     <TextBox Name="tbPersonName" Height="26" Width="222"></TextBox>
144                 </StackPanel>
145                 <!--The default UpdateSourceTrigger property for Text is LostFocus, you will find source will be updated when the Textbox losts focus.
146                     You will see Textbox will not load the defalut value when it is loaded due to it uses OneWayToSouce binding mode.
147                 -->
148                 <StackPanel Orientation="Horizontal"  >
149                     <Label Width="44" Height="25">Job:</Label>
150                     <TextBox Name="tbPersonJob"   Text="{Binding Job, Source={StaticResource defaultPerson}, Mode = OneWayToSource}" Height="26" Width="222"></TextBox>
151                 </StackPanel>
152                 <!---->
153                 <StackPanel Orientation="Horizontal"  >
154                     <Label Height="26" Width="44">Age:</Label>
155                     <TextBox Name="tbPersonAge"  Height="26" Width="222" Validation.ErrorTemplate="{StaticResource errorTemplate}">
156                         <TextBox.Text>
157                             <Binding Path="Age" Source="{StaticResource defaultPerson}">
158                                 <Binding.ValidationRules>
159                                     <local:AgeValidationRule></local:AgeValidationRule>
160                                 </Binding.ValidationRules>
161                             </Binding>
162                         </TextBox.Text>
163                     </TextBox>
164                 </StackPanel>
165                 
166                  <StackPanel Orientation="Horizontal"  >
167                     <Label Height="26" Width="44">Salary:</Label>
168                     <TextBox Name="tbPersonSalary" Text="{Binding Salary, Source={StaticResource defaultPerson}, Mode=Twoway, UpdateSourceTrigger =PropertyChanged}"
169                                                                     Height="26" Width="222"></TextBox>
170                 </StackPanel>
171                 
172                 <!--The target will be updated only if we change the source if the binding mode is Oneway-->
173                 <StackPanel Orientation="Horizontal"  >
174                     <Label Height="23" Width="53">Interest:</Label>
175                     <TextBox Name="tbPersonInterest" Text="{Binding Interest, Source={StaticResource defaultPerson}, Mode =Oneway}" Height="26" Width="222"></TextBox>
176                 </StackPanel>
177             </StackPanel>
178         </Border>
179         <Border Grid.Column="1" Grid.Row="3">
180             <StackPanel Margin="10" >
181                 <StackPanel Orientation="Horizontal">
182                     <Label>Name:</Label>
183                     <Label Name="lblPersonName"  Content="{Binding Name, Source ={StaticResource defaultPerson}}" Height="25" Width="222"></Label>
184                 </StackPanel>
185                 <StackPanel Orientation="Horizontal">
186                     <Label Width="44" Height="25">Job:</Label>
187                     <Label Name="lblPersonJob"   Content="{Binding Job, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
188                 </StackPanel>
189                 <StackPanel Orientation="Horizontal">
190                     <Label Height="26" Width="44">Age:</Label>
191                     <Label Name="lblPersonAge"  Content="{Binding Age, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
192                 </StackPanel>
193                 <StackPanel Orientation="Horizontal"  >
194                     <Label Height="26" Width="44">Salary:</Label>
195                     <Label Name="lblPersonSalary" Content="{Binding Salary, Source={StaticResource defaultPerson}, Converter={StaticResource con}}" Height="26" Width="222"></Label>
196                 </StackPanel>
197                 <StackPanel Orientation="Horizontal">
198                     <Label Height="25" Width="56">Interest:</Label>
199                     <Label Name="lblPersonInterest" Content="{Binding Interest, Source={StaticResource defaultPerson}}" Height="26" Width="222"></Label>
200                 </StackPanel>
201             </StackPanel>
202         </Border>
203         <!--Collection Binding Using Xaml-->
204         <Border Grid.Row="5" Grid.ColumnSpan="2">
205         <ListBox DataContext="{StaticResource personSource}" ItemsSource="{Binding .}">
206                 <ListBox.ItemTemplate>
207                     <DataTemplate>
208                         <StackPanel Orientation="Horizontal">
209                             <Label>Name:</Label>
210                             <Label Content="{Binding Name}"></Label>
211                             <Label>Job:</Label>
212                             <Label Content="{Binding Job}"></Label>
213                             <Label>Interest:</Label>
214                             <Label Content="{Binding Interest}"></Label>
215                         </StackPanel>
216                     </DataTemplate>
217                 </ListBox.ItemTemplate>
218             </ListBox>
219          </Border>
221         <!--Collection Binding Using Procedure code-->
222         <Border Grid.Row="7" Grid.ColumnSpan="2">
223             <ListBox Name="lb">
224                 <ListBox.ItemTemplate>
225                     <DataTemplate>
226                         <StackPanel Orientation="Horizontal">
227                             <Label>Name:</Label>
228                             <Label Content="{Binding Name}"></Label>
229                             <Label>Job:</Label>
230                             <Label Content="{Binding Job}"></Label>
231                             <Label>Interest:</Label>
232                             <Label Content="{Binding Interest}"></Label>
233                         </StackPanel>
234                     </DataTemplate>
235                 </ListBox.ItemTemplate>
236             </ListBox>
237         </Border>
238     </Grid>
239 </Window>